<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :data="data"
      :option="option"
      :loading="loading"
      :page.sync="page"
      @search-change="searchChange"
      @search-reset="searchReset"
      @size-change="sizeChange"
      @current-change="currentChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot-scope="scope" slot="menu">
        <el-button type="text" icon="el-icon-view" size="small" @click.stop="handleViewDetails(scope.row)">详情</el-button>
      </template>
      <!--      <template slot="menuRight">-->
      <!--        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>-->
      <!--      </template>-->
    </avue-crud>
    <!-- 模态框 -->
    <el-dialog append-to-body :visible.sync="detailsDialogVisible" title="详情" width="50%">
      <!-- 这里是模态框内容，根据需要填充数据 -->
      <div>
        <div class="custom-title"><i class="el-icon-vertical-bar mr10"></i>基本信息</div>
        <el-form :model="currentRowDetails">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="客户订单:">
                <el-input v-model="currentRowDetails.sourceNo" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户名称:">
                <el-input v-model="currentRowDetails.vendorName" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- ...其他详情表单项 -->
        </el-form>
        <div class="custom-title"><i class="el-icon-vertical-bar mr10"></i>订单详情</div>
        <avue-crud
          :data="orderDetailData"
          :option="orderDetailOption"
          :loading="orderDetailLoading"
          :page.sync="orderDetailPage"
          @size-change="orderDetailSizeChange"
          @current-change="orderDetailCurrentChange"
        ></avue-crud>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList, getDetail } from '@/api/scm/customer-orders-manage';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
export default {
  name: 'ScmCustomerOrdersManage',
  components: { userTableConfig },
  mixins: [userConfig()],
  data() {
    return {
      query: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      loading: false,
      data: [],
      option: {
        height: 'calc(100vh - 488px)',
        tip: false,
        border: true,
        index: true,
        indexLabel: '序号',
        selection: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        columnBtn: false,
        searchMenuPosition: 'right',
        column: [
          { label: '客户订单', prop: 'sourceNo', search: true },
          { label: '客户名称', prop: 'vendorName' },
          {
            label: '采购日期',
            prop: 'sourceOrderDate',
            search: true,
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间'
          },
          {
            label: '订单状态',
            prop: 'sourceOrderStatus',
            search: true,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=sheet_status',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          { label: '最后同步时间', prop: 'lastSuccessSyncTime' }
        ]
      },
      detailId: '',
      detailsDialogVisible: false, // 控制详情模态框的显示
      currentRowDetails: {}, // 当前行的详细信息
      orderDetailData: [],
      orderDetailOption: {
        height: '400',
        tip: false,
        border: true,
        index: true,
        indexLabel: '序号',
        selection: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        menu: false,
        addBtn: false,
        refreshBtn: false,
        columnBtn: false,
        column: [
          { label: '行号', prop: 'sourceSheetLot' },
          { label: '客户物料编码', prop: 'vendorPartNo' },
          { label: '客户物料名称', prop: 'vendorPartName' },
          { label: '订单数量', prop: 'sourceOrderQuantity' },
          { label: '已发货数量', prop: 'deliveredQuantity' },
          { label: '未发货数量', prop: 'unDeliveredQuantity' }
        ]
      },
      orderDetailPage: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      orderDetailLoading: false
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    onLoad(page, params = {}) {
      if (this.query.sourceOrderDate) {
        this.query.sourceOrderStartDate = this.$moment(this.query.sourceOrderDate[0]).format('YYYY-MM-DD HH:mm:ss');
        this.query.sourceOrderEndDate = this.$moment(this.query.sourceOrderDate[1]).format('YYYY-MM-DD HH:mm:ss');
        delete this.query.sourceOrderDate;
      }
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this._forEach(this.data, (item) => {
          item.sourceOrderStatus = String(item.sourceOrderStatus);
        });
        this.loading = false;
      });
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    handleViewDetails(row) {
      this.detailId = row.sourceId;
      this.detailsDialogVisible = true;
      this.currentRowDetails.sourceNo = row.sourceNo;
      this.currentRowDetails.vendorName = row.vendorName;
      this.orderDetailLoading = true;
      getDetail(this.orderDetailPage.currentPage, this.orderDetailPage.pageSize, Object.assign({ sourceId: row.sourceId })).then((res) => {
        const data = res.data.data;
        this.orderDetailPage.total = data.total;
        this.orderDetailData = data.records;
        this.orderDetailLoading = false;
      });
    },
    orderDetailSizeChange(pageSize) {
      this.orderDetailPage.pageSize = pageSize;
      this.handleViewDetails(this.detailId);
    },
    orderDetailCurrentChange(currentPage) {
      this.orderDetailPage.currentPage = currentPage;
      this.handleViewDetails(this.detailId);
    }
  }
};
</script>

<style scoped lang="scss">
.custom-title {
  font-size: 14px;
  font-weight: bold;
  margin: 10px 0;
  i {
    margin-right: 10px;
  }
}
::v-deep .el-date-editor {
  justify-content: space-between;
  .el-input__icon {
    display: none;
  }
  input {
    width: 43%;
  }
}
::v-deep .el-dialog__body {
  .avue-crud__menu {
    display: none;
  }
}
</style>
